Fluent Interfaces & Method Chaining

time to read 1 min | 191 words

Hammett calls the term "Fluent Interface" unnecessary:

And about Fluent interfaces, what about OOP? What about method chaining? Does it need, for god’s sake, a new name?

This is method chaining:

string user = new StringBuilder()
	.Append("Name: ")
	.Append(user.Name)
	.AppendLine()
	.Append("Email: ")
	.Append(user.Email)
	.AppendLine()
	.ToString();	

And this is a fluent interface:

return new Finder<Order>(
		Where.Order.User == CurrentUser &&
		(
			Where.Order.TotalCost > Money.Dollars(150) ||
			Where.Order.OrderLines.Count > 15
		),
		OrderBy.Order.CreatedAt
	).List();

Anders posts about DSL building contains examples that are tied more closely to the domain.

Method chaining is something that you would certainly use in the a fluent interface, but that is like saying that you need to use interfaces when you build a plugin framework. The fact that you are using something doesn't mean that what you do is only that something.

Fluent interfaces are different than merely method chaining because they allow you to express your intent in the domain terms and allows you to get more readable code. Method chaining, operator overloading, nasty generics tricks are all part of that, certainly, but the end result is much more than just a simple method chain.

And yes, I think that it certainly deserves its own name.